package com.lbl.springboot_study.Controller;

import com.lbl.springboot_study.domain.Person;
import com.lbl.springboot_study.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 李柏霖
 * 2020/11/19 21:41
 */

@Controller
public class PersonController {

    @Autowired
    HttpServletRequest request;

    @RequestMapping(path = "/test01", method = {RequestMethod.GET})
    public String test01() {
        //name jack
        request.setAttribute("name", "jack");
        return "person-list";
    }


    @RequestMapping(path = "/test02", method = {RequestMethod.GET})
    public String test01(ModelMap modelMap) { //带数据建议大家使用ModelMap
        //name jack
        modelMap.addAttribute("name", "jack");

        //准备三个人的数据放到页面
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setUsername("jack" + i);
            user.setPassword("123456");
            list.add(user);
        }
        //添加数据
        modelMap.addAttribute("list", list);
        return "person-list"; //classpath:/template/person-list.html
    }
}


